package com.sitech.portal.server.service.impl;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.ibnms.util.RandomGUID;
import com.sitech.portal.server.dao.INewsDao;
import com.sitech.portal.server.service.INewsService;

import java.io.File;
import java.util.HashMap;
import java.util.List;


public class NewsServiceImpl implements INewsService {

	private INewsDao newsDao;
    private String saveFilePath = "newsFile";


    public void setNewsDao(INewsDao newsDao) {
        this.newsDao = newsDao;
    }


    @Override
    public void insertNews(HashMap<String,String> news) {
        if(news.get("ID")==null || news.get("ID").equals("")){
            news.put("ID", RandomGUID.getRandomGUID());
        }else{
            newsDao.deleteNews(news.get("ID"));
            newsDao.deleteNewsDept(news.get("ID"));
            newsDao.deleteNewsUser(news.get("ID"));
            newsDao.deleteNewsFileRel(news.get("ID"),null);
        }
        newsDao.insertNews(news);
        if(news.get("RCV_DEPT_ID")!=null && !news.get("RCV_DEPT_ID").equals("")){
            String [] stringArr= news.get("RCV_DEPT_ID").split(",");
            for (int i = 0; i < stringArr.length; i++) {
                newsDao.insertNewsDept(news.get("ID"),stringArr[i]);
                newsDao.insertNewsDeptUser(news.get("ID"), stringArr[i]);
            }
        }

        if(news.get("RCV_LOGIN_ID")!=null && !news.get("RCV_LOGIN_ID").equals("")){
            String [] stringArr= news.get("RCV_LOGIN_ID").split(",");
            for (int i = 0; i < stringArr.length; i++) {
                newsDao.insertNewsUser(news.get("ID"), stringArr[i]);
            }
        }

        if(news.get("FILE_IDS")!=null && !news.get("FILE_IDS").equals("")){
            String [] stringArr= news.get("FILE_IDS").split(",");
            for (int i = 0; i < stringArr.length; i++) {
                newsDao.insertNewsFile(news.get("ID"), stringArr[i]);

            }
        }
    }

    @Override
    public IPage selectNewsList(HashMap map, PageForm pageForm) {
        return newsDao.selectNewsList(map,pageForm);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public HashMap selectNews(String id) {
        return newsDao.selectNews(id);
    }


    @Override
    public IPage selectHisNewsList(HashMap map, PageForm pageForm) {
        return newsDao.selectHisNewsList(map,pageForm);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void updatePopNews(String userid, String id) {
        newsDao.updatePopNewsStatus(userid, id);
    }

    @Override
    public boolean canBeModify(String newsid) {
        return newsDao.canBeModify( newsid);
    }


    @Override
    public void deleteNews(List<String> ids,String loginid) {
        for (int i = 0; i < ids.size(); i++) {
            newsDao.updateNewsStatus(ids.get(i), loginid);
//            newsDao.deleteNewsDept(ids.get(i));
//            newsDao.deleteNewsUser(ids.get(i));
        }

    }

    @Override
    public List selectUnReadNews(String userid) {
        return newsDao.selectUnReadNews(userid);
    }

    @Override
    public void updateReadNews(String userid, List<String> ids) {
        for (int i = 0; i < ids.size(); i++) {
            newsDao.updateReadNews(ids.get(i),userid);
        }
    }

    @Override
    public HashMap saveNewsFileDb(HashMap<String,String> map) {
        int sizeInt = Integer.valueOf(map.get("FILE_SIZE"));
        if(sizeInt/1024<1){
            map.put("FILE_SIZE", sizeInt+"B");
        }else if(sizeInt/(1024*1024)<1){
            map.put("FILE_SIZE", sizeInt/1024+"KB");
        }
        else if(sizeInt/(1024*1024*1024)<1){
            map.put("FILE_SIZE", sizeInt/(1024*1024)+"M");
        }
        if(map.get("GUID") == null){
            map.put("GUID",RandomGUID.getRandomGUID());
        }
        filePathToUrlPath(map);
        newsDao.saveNewsFileDb(map);
        return map;
    }

    @Override
    public void deleteNewsFileRel(String newsid, String fileid) {
        newsDao.deleteNewsFileRel(newsid, fileid);
    }

    /**
     * 将远程服务器的目录转换成url相对路径
     */
    private void filePathToUrlPath(HashMap<String,String> fileMap){
        String remoteFilePath=fileMap.get("FILE_PATH");
        String urlFilePath=remoteFilePath;
        if(remoteFilePath.indexOf(File.separator+saveFilePath)>0){
            urlFilePath=remoteFilePath.substring(remoteFilePath.indexOf(File.separator+saveFilePath));
            if("\\".equals(File.separator)){
                urlFilePath=urlFilePath.replaceAll("\\\\","/");
            }
        }
        fileMap.put("FILE_PATH",urlFilePath);

    }

}
